﻿<template>
  <el-row class="coupon-upload">
    <div class="title">
      <span v-if="title">{{ title }}</span>
    </div>
    <el-row type="flex" justify="space-between">
      <el-upload
        v-loading="uploading"
        :action="action"
        :data="formData"
        :on-error="uploadFail"
        :before-upload="beforeUploadHandler"
        :show-file-list="false"
        :on-success="handleSuccess"
      >
        <el-button size="small" type="primary">
          批量添加优惠券
        </el-button>
      </el-upload>
      <el-button type="text" @click="download()">
        下载优惠券模版
      </el-button>
    </el-row>
    <el-row v-if="couponFileName">
      <el-col :span="12">
        <el-link
          class="padding_a"
          type="primary"
          @click="downCouponList"
        >
          {{ couponFileName }}
        </el-link>
      </el-col>
      <el-col :span="8">
        <el-button
          type="text"
          @click="openCouponList"
        >
          查看优惠券详情
        </el-button>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-delete padding_a" @click="$emit('handleRemove')"></i>
      </el-col>
    </el-row>
    <coupon-list
      :request-obj="formData"
      :coupon-list-is-show="couponListIsShow"
      @close="closeCouponList"
      @updateCouponInfo="$emit('updateCouponInfo')"
    />
  </el-row>
</template>

<script>
import CouponList from '../CouponList'
import tool from '@/util/tool'

export default {
  name: 'CouponUpload',
  components: {
    CouponList
  },
  props: {
    title: {
      type: String,
      required: false,
      default: ''
    },
    formData: {
      type: Object,
      default: () => {}
    },
    couponFileName: {
      type: String,
      default: () => ''
    },
    beforeUpload: {
      type: [Function, Boolean],
      required: false,
      default: false
    }
  },
  data() {
    return {
      uploading: false,
      couponListIsShow: false,
      action: '/coupon/uploadCouponInfo',
      downloadAction: '/coupon/downloadTemplate',
      downloadCoupon: '/coupon/downloadCouponInfo'
    }
  },
  computed: {
    beforeUploadHandler () {
      return this.beforeUpload || this.beforeUploadFn
    }
  },
  mounted() {
    const shiroId = tool.getUrlParam('shiro_sid')
    this.action = `${this.$URLS.MOS_API_HOST}${this.action}?shiro_sid=${shiroId}`
    this.downloadAction = `${this.$URLS.MOS_API_HOST}${this.downloadAction}?shiro_sid=${shiroId}`
    this.downloadCoupon = `${this.$URLS.MOS_API_HOST}${this.downloadCoupon}?shiro_sid=${shiroId}`
  },
  methods: {
    handleSuccess (res, file) {
      this.uploading = false
      if (res?.flag === 'S') {
        this.$message.success('上传文件成功')
        this.$emit('handleSuccess', file.name)
      } else {
        this.$message.error('导入失败')
      }
    },
    uploadFail() {
      this.uploading = false
      this.$message.error('上传失败,请重试')
    },
    download () {
      tool.downloadByForm(this.downloadAction)
    },
    downCouponList () {
      tool.downloadByForm(this.downloadCoupon, {
        ...this.formData,
        filename: this.couponFileName
      })
    },
    beforeUploadFn (file) {
      let arr = file.name.split('.')
      if (arr.length >= 2) {
        let ext = arr[arr.length - 1].toLowerCase()
        if (ext === 'xls' || ext === 'xlsx') {
          this.uploading = true
          return true
        } else {
          this.$message.error('上传文件只能是 xls 或 xlsx 格式!')
          return false
        }
      } else {
        this.$message.error('文类型不明确!')
        return false
      }
    },
    openCouponList () {
      this.couponListIsShow = true
    },
    closeCouponList () {
      this.couponListIsShow = false
    }
  }
}
</script>

<style lang="less" scoped>
.coupon-upload {
  margin-top: 0px;
  padding-bottom: 10px;
  border-bottom: 1px solid #cfd7e3;
  .title {
    padding: 5px 0;
  }
  p {
    padding: 0;
    color: rgba(84, 155, 231, 1);
    cursor: pointer;
  }
  .padding_a {
    padding: 0.24rem 0rem;
    text-decoration: underline;
    color: #428bca
  }
}
</style>
